import * as bm from "/js/baseModule.js";
// require('/js/autocomplete.min.js');

export class Module extends bm.baseModule {
    constructor(view) {
        super(view);
        this._flag = false;
        this._sendImgPending = this.find(".sendImgPending");
        let vueDom = this.find("#BasicInfo_vue").get(0);
        let user = this.user = globalData.user;
        let that = this;

        let BasicInfo_vue = this.BasicInfo_vue = new Vue({
            el: vueDom,
            data: {
                user: user,
                checkcompany: 3
            },
            watch: {
                'checkcompany': "checkCompany",
            },
            methods: {
                checkCompany: function () {
                    if (BasicInfo_vue.checkcompany === 3) {
                        //公司
                        $('.personal-tip').addClass('hide');
                        $('.company-tip').removeClass('hide');
                    } else if (BasicInfo_vue.checkcompany === 4) {
                        //个人
                        $('.company-tip').addClass('hide');
                        $('.personal-tip').removeClass('hide');
                    }
                }
            }
        });
        /** 公司名称自动匹配填充  */
        // $.getJSON('/company.json', function (json) {
        //     that._json = json;
        //     $(".input-div").autocomplete({
        //         source: json.com,
        //         select: function (event, ui) {
        //             $(this).val(ui.item.label);
        //             that.BasicInfo_vue.user.company_name = ui.item.label;
        //         }
        //     })
        // }, function (error) {
        //     console.log(error)
        // });
        /*监听数据返回*/
        $(document).on('data_update', function () {
            that._flag = true;
            if (!(that._sendImgPending.hasClass('hide'))) {
                that._evt_joinNext();
            }
        });
        if (globalData.ipPos.prov_id != "0") {
            that._flag = true;
        }
        /*初始化时按钮不可点击*/
        this.find(".btn").attr("disabled", "false");

    }

    afterInstall() {

    }

    checkNick(info, dom, tip) {
        let reg = /^[\u4e00-\u9faf]+$/
        if (info == "") {
            funs.topTip(tip + '不能为空！');
            return false;
        } else if (!reg.test(info)) {
            funs.topTip('请输入正确的' + tip);
            return false;
        } else {
            return true;
        }
    }

    /**手机号 */
    checkPhone(info, dom, tip) {
        let reg = /^1(3|4|5|7|8)\d{9}$/
        if (info == "") {
            funs.topTip(tip + '不能为空！');
            return false;
        } else if (!reg.test(info)) {
            funs.topTip('请输入正确的' + tip);
            return false;
        } else {
            return true;
        }
    }

    /**公司名称校验 */
    checkCompany(info, dom, tip) {
        if (info == "") {
            funs.topTip(tip + '不能为空！');
            return false;
        }
        return true;
    }

    /**提交 */
    _evt_joinNext() {
        if (this.checkNick(globalData.user.nick, 'nick', "昵称")) {
            /**
             * 加盟成为工程商
             */
            let that = this;
            let nickname = globalData.user.nick;
            let gender = globalData.user.sex;
            let prov = globalData.ipPos.prov;
            let city = globalData.ipPos.city;
            let com = globalData.user.company_name;
            if (that.BasicInfo_vue.checkcompany == 4) {
                com = '';
            } else if (that.BasicInfo_vue.checkcompany == 3 && !(that.checkCompany(com, 'company', "公司全称"))) {
                return;
            }
            let userInfo = {
                nickname: nickname,
                gender: gender,
                prov: prov,
                city: city,
                com: com,
            };
            if (!that._flag) {
                that._sendImgPending.removeClass('hide');
                return;
            }
            /**加盟工程商接口 */
            api.certifySupplier(userInfo, {
                succ:function () {
                    //成功，刷新
                    funs.tip('认证成功', "", "succ");
                    setTimeout(function () {
                        window.location.href = '/index.html';
                    }, 2020);
                    window.location.href = '/index.html';
                },
                fail: function (error) {
                    if (error.status == '401') {
                        //未登录，去执行登录
                        return
                    } else if (error.status == '41009031') {
                        funs.tip(error.msg, "", "fail");
                        setTimeout(function () {
                            window.location.href = '/index.html';
                        }, 2020);
                    } else {
                        funs.tip(error.status, error.msg, "fail");
                    }
                }
            });
        }
    }

    /**必须选择用户协议*/
    _evt_choose(data, obj) {
        $(obj).find('.cricle').toggleClass("active");
        if (this.find('.active').length != 0 && this.find('.active')[0].getAttribute("type") == '1') {
            this.find(".btn").removeAttr("disabled");
        } else {
            this.find(".btn").attr("disabled", "false");
        }
    }

    /**打开用户协议*/
    _evt_openAgreement() {
        location.hash = 'about/UserAgreement';
    }
}
